/*
 * Created on Oct 11, 2004
 */
package com.hermeneute.fsa.editors.contentassist;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.jface.text.rules.IPredicateRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;

import com.hermeneute.fsa.parser.FSTParserTreeTK;

/**
 * @author Phil Zoio
 */
public class FSAContentAssistProcessor implements IContentAssistProcessor
{
	Set<String> proposals = new HashSet<String>();
	int maxOffset=0;
	
    public FSAContentAssistProcessor() {
        super();
    }

    void refreshProposals(IDocument document,int offset) {
    	String content = document.get().substring(0,offset);
    	FSTParserTreeTK ptk = new FSTParserTreeTK(content);
		String tmp[] = ptk.getAutomata();
		for (int i = 0;i < tmp.length;i++) {
			proposals.add(tmp[i].substring(1));
		}
    }
    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {   
    	String fp[] = {":\""};
    	IDocument document = viewer.getDocument();
    	String content = document.get();
     	if ("<".equals(content.substring(offset-1, offset))) {
    		String proposals[] = new FSTParserTreeTK(content).getAutomata();
       		ICompletionProposal[] result =
		        new ICompletionProposal [proposals.length];
       		
       		for (int i = 0;i<proposals.length;i++) {
       			result[i] = new CompletionProposal(proposals[i].substring(1),offset,0,fp.length);
       		}
       		return result;
    	} else if ("0".equals(content.substring(offset-1, offset))) {
    		ICompletionProposal[] result =
    		        new ICompletionProposal [fp.length];
    		for (int i = 0; i < fp.length;i++) {
    			result[i] = new CompletionProposal(fp[i],offset,0,fp.length);
    		}
    		return result;
    	} else {
    		return null;
    	}
    }

    public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) {
		return null;
	}

	public char[] getCompletionProposalAutoActivationCharacters() {
		char res[] = {'<','0'};
	    return res;
	}

	public char[] getContextInformationAutoActivationCharacters() {
		return null;
	}

	public IContextInformationValidator getContextInformationValidator() {
		return null;
	}

	public String getErrorMessage() {
		return null;
	}
}